use eframe::emath::vec2;
use eframe::epaint::{Color32, FontId};
use egui::{Button, ComboBox, RichText, TextEdit, Ui};

pub(crate) fn create_edit_ui(ui: &mut Ui, eidt_width: f32, edit_str: &mut String) {
    create_edit_ui_wh(ui,eidt_width, 200.0, edit_str);
}

pub(crate) fn create_edit_ui_wh(ui: &mut Ui, eidt_width: f32, eidt_height: f32, edit_str: &mut String) {
    ui.add_sized(
        vec2(eidt_width, eidt_height),
        TextEdit::multiline(edit_str)
            .hint_text("请输入...")
            .font(eframe::epaint::FontId::proportional(18.0))
            .background_color(Color32::from_rgb(235,235,235))
            .desired_width(f32::INFINITY),
    );
}

pub(crate) fn create_edit_height_ui(
    ui: &mut Ui,
    eidt_width: f32,
    eidt_height: f32,
    edit_str: &mut String,
) {
    ui.add_sized(
        vec2(eidt_width, eidt_height),
        TextEdit::multiline(edit_str)
            .hint_text("请输入...")
            .font(eframe::epaint::FontId::proportional(14.0))
            .desired_width(f32::INFINITY),
    );
}

pub(crate) fn create_button_ui<F>(ui: &mut Ui, content: &str, fn_click: F)
where
    F: FnOnce(),
{
    let rich_text = RichText::new(content)
        .font(FontId::proportional(14.0))
        .color(Color32::WHITE);

    let button = Button::new(rich_text)
        .fill(Color32::from_rgb(99,125,253))
        .corner_radius(8);
    if ui.add_sized(vec2(100.0, 40.0), button).clicked() {
        fn_click();
    }
}

pub(crate) fn create_combo_box_ui(ui: &mut Ui, mut selection: usize, items: &[&'static str]) {
    let select_t1 = RichText::new(items[selection])
        .font(FontId::proportional(15.0))
        .color(Color32::BLACK);
    ComboBox::from_label("")
        .selected_text(select_t1)
        .show_ui(ui, |ui| {
            for item in 0..items.len() {
                ui.selectable_value(&mut selection, item, items[item]);
            }
        });
}


#[macro_export]
macro_rules! macro_create_combo_box_ui {
     ($ui:expr, $id_salt:expr, $sel:expr, $items:expr) => {
         let select_t1 = RichText::new($items[$sel].clone())
        .font(FontId::monospace(15.0))
        .color(Color32::BLACK);
    ComboBox::new($id_salt, "")
        .selected_text(select_t1)
        .show_ui($ui, |ui| {
            for item in 0..$items.len() {
                ui.selectable_value(&mut $sel, item, $items[item].clone());
            }
        });
     }
}

#[macro_export]
macro_rules! macro_create_combo_box_ui_changed {
     ($ui:expr, $id_salt:expr, $sel:expr, $items:expr, $change_fn:expr) => {
         let select_t1 = RichText::new($items[$sel].clone())
        .font(FontId::monospace(15.0))
        .color(Color32::BLACK);
   if ComboBox::new($id_salt, "")
        .selected_text(select_t1)
        .show_ui($ui, |ui| {
            for item in 0..$items.len() {
                ui.selectable_value(&mut $sel, item, $items[item].clone());
            }
        }).response.changed() {
             $change_fn();
         }
     }
}